package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NetWorkDialing {

    public static String dialAndRetrieveInterfaceName(String connectionName, String username, String password) throws Exception {
        String osName = System.getProperty("os.name").toLowerCase();
        String ipAddress = null;
        String interfaceName = null;

        if (osName.contains("win")) {
            // Windows 拨号
            Process dialProcess = Runtime.getRuntime().exec("rasdial " + connectionName + " " + username + " " + password);
            BufferedReader reader = new BufferedReader(new InputStreamReader(dialProcess.getInputStream(), "GBK")); // 使用GBK编码
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            dialProcess.waitFor();

            // 获取IP地址和网卡名称
            Process ipProcess = Runtime.getRuntime().exec("ipconfig");
            BufferedReader ipReader = new BufferedReader(new InputStreamReader(ipProcess.getInputStream(), "GBK")); // 使用GBK编码
            String ipLine;

            while ((ipLine = ipReader.readLine()) != null) {
                if (ipLine.contains(connectionName)) {
                    interfaceName = connectionName;
                }
                if (interfaceName != null && ipLine.contains("IPv4")) {
                    String[] parts = ipLine.split(":");
                    if (parts.length > 1) {
                        ipAddress = parts[1].trim();
                        System.out.println("IP Address: " + ipAddress);
                        return interfaceName;
                    }
                }
            }

            ipProcess.waitFor();
        } else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
            // Linux 拨号
            Process dialProcess = Runtime.getRuntime().exec("sudo pon " + connectionName + " user " + username + " password " + password);
            BufferedReader reader = new BufferedReader(new InputStreamReader(dialProcess.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            dialProcess.waitFor();

            // 获取IP地址和网卡名称
            Process ipProcess = Runtime.getRuntime().exec("ip addr show");
            BufferedReader ipReader = new BufferedReader(new InputStreamReader(ipProcess.getInputStream()));
            String ipLine;

            while ((ipLine = ipReader.readLine()) != null) {
                if (ipLine.contains(connectionName)) {
                    interfaceName = connectionName;
                }
                if (interfaceName != null && ipLine.contains("inet")) {
                    String[] parts = ipLine.split(" ");
                    for (String part : parts) {
                        if (part.contains("/")) {
                            ipAddress = part.split("/")[0];
                            System.out.println("IP Address: " + ipAddress);
                            return interfaceName;
                        }
                    }
                }
            }

            ipProcess.waitFor();
        } else {
            throw new Exception("Unsupported operating system: " + osName);
        }

        return null;
    }

    public static void main(String[] args) {
        try {
            String connectionName = "eth38";
            String username = "MZFTTH075300380424@16900.gd";
            String password = "123456";
            String interfaceName = dialAndRetrieveInterfaceName(connectionName, username, password);
            System.out.println("Interface Name: " + interfaceName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String connectionName = "eth39";
            String username = "MZFTTH075300380424@16900.gd";
            String password = "123456";
            String interfaceName = dialAndRetrieveInterfaceName(connectionName, username, password);
            System.out.println("Interface Name: " + interfaceName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
